/*
 * @Author: Lin Hongwei
 * @Date: 2022-10-30 20:35:18
 * @LastEditTime: 2022-10-30 20:48:47
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 
 */
const { query, create, update, delete: deleteTask } = require("../service/taskType.service")

class TaskTypeController {
  async getTaskTypeList(ctx, next) {
    try {
      const { id, name } = ctx.request.body;
      let params = null;
      if (id || name) {
        params = {};
        id && (params.id = id);
        name && (params.name = name);
      }
      const res = await query(params);
      ctx.success(res);
    } catch (err) {
      ctx.fail("查询任务类型失败！")
    }
  }
  async createTaskType(ctx, next) {
    try {
      const res = await create(ctx.request.body);
      if (!!res.affectedRows) return ctx.success();
      ctx.fail("创建任务类型失败!");
    } catch (err) {
      ctx.fail("查询任务类型失败！")
    }
  }
  async updateTaskType(ctx, next) {
    let data = ctx.request.body;
    let params = [data, { id: data.id }];
    delete params[0].id;
    try {
      const res = await update(params);
      if (res.affectedRows > 0) return ctx.success();
      ctx.fail("更新任务类型失败!")
    } catch (err) {
      ctx.fail("更新任务类型失败!")
    }
  }
  async deleteTaskType(ctx, next) {
    try {
      const res = await deleteTask(ctx.request.body)
      if (!!res.affectedRows) return ctx.success();
      ctx.fail("删除任务类型失败!")
    } catch (err) {
      ctx.fail("删除任务类型失败!");
    }
  }
}

module.exports = new TaskTypeController();